<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<script type="text/javascript" src="js/jquery.itextclear.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $('input[type=text], input[type=password], input[type=url], input[type=email], input[type=number], textarea', '.form').iTextClear();
    });
</script>
<!-- jQplot CSS -->

<link rel="stylesheet" media="screen" href="CSS/Tree.css"/>
<!-- jQplot CSS END -->

<!-- DATATABLES CSS -->
<link rel="stylesheet" media="screen" href="lib/datatables/css/vpad.css"/>
<script type="text/javascript" src="lib/datatables/js/jquery.dataTables.js"></script>
<script type="text/javascript" src="${ctx}/js/global_util.js"></script>

<!-- DATATABLES CSS END -->


<h1 class="page-title">${listTitle}</h1>

<div class="container_12 clearfix leading" style="margin-top:0px">
    <section class="tabs grid_12 leading">
        <ul class="clearfix">
            <li><a href="#" class="current">分值上升</a></li>
            <li><a href="#">分值下降</a></li>
            <li><a href="#">其他异常</a></li>
        </ul>
        <section>

            <section class="clearfix" style="display: block;">
                <header class="grid_12 alpha omega">
                    <p>
                        筛选： <select name="sel_periodtype" id="sel_periodtype_up" style="opacity: 0;"
                                    onchange="loadLastThirdExamPeriod(this,'up')">
                        <option value="none">所有周期</option>
                        <option value="monthly">月度</option>
                        <option value="bimonthly">双月</option>
                        <option value="season">季度</option>
                        <option value="semiyearly">半年</option>
                        <option value="annually">一年</option>
                    </select>
                        <select name="sel_period" id="sel_period_up" style="opacity: 0;">
                            <option value="-1">所有周期</option>
                        </select><span> <select name="sel_area" id="sel_area_up"
                                                style="opacity: 0;" onchange="loadChildrenOrgsByTab(this)">
                        <option value="-1">所有分公司</option>
                        <s:iterator value="#request.organizations" var="orgs" status="st">
                            <option value="${orgs.id}">${orgs.fullName}</option>
                        </s:iterator>
                    </select></span>
                        请输入分值：
                        <input name="fromscore" type="text" id="from_score_up" size="3"/> 分 - <input name="toscore"
                                                                                                     type="text"
                                                                                                     id="to_score_up"
                                                                                                     size="3"/> 分
                        <button onclick="checkInput('up')"><span style="background:url(${ctx}/images/img/zoom.png)"
                                                                 class="icon">&nbsp;</span>确认搜索
                        </button>
                    </p>
                </header>
            </section>
            <section class="clearfix" style="display: none;">
                <header class="grid_12 alpha omega">
                    <p>
                        筛选： <select name="sel_periodtype" id="sel_periodtype_down" style="opacity: 0;"
                                    onchange="loadLastThirdExamPeriod(this,'down')">
                        <option value="none">所有周期</option>
                        <option value="monthly">月度</option>
                        <option value="bimonthly">双月</option>
                        <option value="season">季度</option>
                        <option value="semiyearly">半年</option>
                        <option value="annually">一年</option>
                    </select>
                        <select name="sel_period" id="sel_period_down" style="opacity: 0;">
                            <option value="-1">所有周期</option>
                        </select><span> <select name="sel_area" id="sel_area_down"
                                                style="opacity: 0;" onchange="loadChildrenOrgsByTab(this)">
                        <option value="-1">所有分公司</option>
                        <s:iterator value="#request.organizations" var="orgs" status="st">
                            <option value="${orgs.id}">${orgs.fullName}</option>
                        </s:iterator>
                    </select></span>
                        请输入分值：
                        <input name="fromscore" type="text" id="from_score_down" size="3"/> 分 - <input name="toscore"
                                                                                                       type="text"
                                                                                                       id="to_score_down"
                                                                                                       size="3"/> 分
                        <button onclick="checkInput('down')"><span style="background:url(${ctx}/images/img/zoom.png)"
                                                                   class="icon">&nbsp;</span>确认搜索
                        </button>
                    </p>
                </header>
            </section>
            <section class="clearfix" style="display: none;">
                <header class="grid_12 alpha omega">
                    <p>

                        选择异常情况：
                        <select id="form-timezone" style="opacity: 0;">
                            <option value="1">考勤异常</option>
                            <option value="2">事假异常</option>
                            <option value="3">病假异常</option>
                            <option value="3">新进员工</option>
                        </select> - <a class="button" href="#"><span
                            style="background:url(${ctx}/images/img/zoom.png)" class="icon">&nbsp;</span>确认搜索</a>
                    </p>
                </header>
            </section>
        </section>
    </section>
</div>


<div class="container_12 clearfix leading" style="margin-top:0px">
    <div class="tabs side grid_12 leading" id="list_content_facetalk">
        <h2>暂无面谈数据</h2>
    </div>
</div>


<div class="clear"></div>

<script type="text/javascript">
function modifyFaceStatus(id) {
    if (id == null || id == 0) {
        return;
    }
    var url = "${ctx}/ajax/modify_facetime_status.xhtml";
    $.ajax(url, {
        type: "POST",
        data: {staId:id},
        dataType:"html",
        cache:false,
        success:function(a) {
            if (a) {
                alert('面谈成功');
                var obj = $("#exam_" + id);
                obj.html("");
                obj.append("<a class=\"button\" href=\"javascript://\"><span style=\"background:url(${ctx}/images/img/user_comment.png)\" class=\"icon\">&nbsp;</span>已面谈</a>");
            } else {
                alert('面谈失败');
            }

        },
        complete:function() {
        }
    });
}
function checkInput(type) {
    if ($("#sel_periodtype_" + type).val().trim() == "none") {
        alert("请选择考核周期类型");
        return;
    }
    //开始分值
    var fromscore = $("#from_score_" + type).attr("value");
    if (checkNull(fromscore)) {
        alert('请输入开始分值');
        //$("#from_socre").focus();
        return false;
    }
    if (isNaN(fromscore)) {
        alert('请填写正确的开始分值');
        $("#from_score_" + type).attr("value", "");
        return false;
    }

    if (fromscore < 0) {
        alert('分值必须大于0');
        $("#from_score_" + type).attr("value", "");
        return false;
    }
    //结束分值
    var toscore = $("#to_score_" + type).attr("value");
    if (checkNull(toscore)) {
        alert("请输入结束分值");
        return false;
    }

    if (isNaN(toscore)) {
        alert('请填写正确的结束分值');
        $("#to_score_" + type).attr("value", "");
        return false;
    }

    if (toscore < 0) {
        alert('分值必须大于0');
        $("#to_score_" + type).attr("value", "");
        return false;
    }
    if (toscore < fromscore) {
        alert('开始分不能大于结束分');
        return false;
    }

    var url = "${ctx}/exam/facetalk_list.xhtml";
    var data = {"organizationId":currentSelOrgId,
        "type":$("#sel_periodtype_" + type).val(),
        "periodDesc":$("#sel_period_" + type).val(),
        "startScore":fromscore,
        "endScore":toscore,
        "status":type
    };
    $.ajax(url, {
        type: "POST",
        data: data,
        dataType:"html",
        cache:false,
        success:function(a) {
            var obj = $("#list_content_facetalk");
            obj.html("");
            obj.append(a);
        },
        complete:function() {
        }
    });
}

function loadLastThirdExamPeriod(tarObj, type) {
    var ptype = tarObj.options[tarObj.selectedIndex].value;
    if (true) {
        initPeriodByType(ptype, type);
        return;
    }

    //periods
    $.ajax({
        type:"get",
        url:"/ajax/period_list.xhtml?periodEnum=" + ptype,
        //data: "name=John&location=Boston",
        dataType:"json",
        success:function (data) {
            var strHTML = "<option value=\"-1\">当前考核周期</option>";
            $.each(data, function (i, period) {
                strHTML += "<option value=\"" + period.id + "\">" + org.periodName + "</option>";
            });
            $("#sel_period_" + type).html(strHTML);
        }
    });
}

var currentSelOrgId = -1;
function loadChildrenOrgsByTab(targetObj) {
    currentSelOrgId = targetObj.options[targetObj.selectedIndex].value;
    $(targetObj).prev().html($(targetObj).find("option:selected").text());
    $(targetObj).parent().nextAll().remove();
    if (currentSelOrgId != -1) {
        //load the organization uniform-sel_area
        $.ajax({
            type:"get",
            url:"/ajax/organization-children.xhtml?parentId=" + currentSelOrgId,
            //data: "name=John&location=Boston",
            dataType:"json",
            success:function (data) {
                if (data != null && data.length > 0) {
                    var strHTML = " <div id=\"uniform-sel-area-" + currentSelOrgId + "\" class=\"selector\">";
                    strHTML += "<span style=\"-moz-user-select: none;\">所有</span>";
                    strHTML += "<select id=\"sel_area-" + currentSelOrgId + "\" name=\"sel_area-" + currentSelOrgId + "\" style='opacity: 0;' onchange='loadChildrenOrgsByTab(this)'>";
                    strHTML += "<option value=\"-1\">所有</option>";
                    $.each(data, function (i, org) {
                        strHTML += "<option value=\"" + org.id + "\">" + org.fullName + "</option>";
                    });
                    strHTML += "</select></div>";
                    $(targetObj).parent().after(strHTML);
                }
            }
        });
    }
}

var bimonthlys_p = ["11~12","1~2","3~4","5~6","7~8","9~10"];
var seasons_p = ["10~12","1~3","4~6","7~9"];
var semiyearly_p = ["7~12","1~6"];
function initPeriodByType(type, status) {
    if (type == "none") {
        $("#sel_period_" + status).prev("span").html("所有周期");
        return;
    }

    var myDate = new Date();
    var d = myDate.getDate(); //1~31
    var m = myDate.getMonth();//0~11
    var y = myDate.getFullYear();//2012
    var options = new MyObj(type, m + 1, y).options;

    if (options.length > 0) {
        var obj = $("#sel_period_" + status);
        obj.html("");
        for (var i = 0; i < options.length; i++) {
            obj.append("<option value=\"" + options[i].value + "\">" + options[i].title + "</option>")
        }
        $("#sel_period_" + status).prev("span").html(options[0].title);
    }
}

function MyObj(type, mon, year) {
    this.type = type;
    this.m = mon;
    this.y = year;

    function getOptionsByType(type, mon, year) {
        var options = new Array();
        //options.push({"title":"当前考核周期","value":"-1"});
        if (type == "monthly") {//单月
            options.push({"title":"当前考核周期","value":"-1"});
            var j = 0;
            var mm = mon - 2;
            var yy = year;
            while (j < 3) {
                if (mm <= 0) {
                    mm = 12 + mm;
                    yy = yy - 1;
                }
                options.push({"title":yy + "年" + mm + "月","value":yy + "-" + mm});
                //options.push('<option value="'+yy+'_'+mm+'">'+yy+'年'mm+'月</option>');
                j++;
                mm--;
            }

        } else if (type == "bimonthly") {//双月
            var yy = year;
            var index = (mon % 2 == 0) ? (mon - 1) / 2 : mon / 2;
            index = parseInt(index);
            var j = 0;
            if (mon % 2 == 1) {
                options.push({"title":"当前考核周期","value":"-1"});
                index--;
                if (index < 0) {
                    index = 5;
                    yy--;
                }
            }

            while (j < 3) {
                if (index <= 0) {
                    yy--;
                }
                options.push({"title":yy + "年" + bimonthlys_p[index] + "月","value":yy + "-" + bimonthlys_p[index].split("~")[1]});
                j++;
                if (index <= 0) {
                    index = 5;
                } else {
                    index--;
                }
            }
        } else if (type == "season") {//季度
            var yy = year;
            var temp = mon % 3;
            if (temp == 1) {//1,4,7,10可考核
                index = mon / 3;
            } else {
                index = (mon - temp) / 3;
            }
            index = parseInt(index);
            var j = 0;
            if (temp == 1) {
                options.push({"title":"当前考核周期","value":"-1"});
                index--;
                if (index < 0) {
                    index = 3;
                    yy--;
                }
            }
            while (j < 3) {
                if (index <= 0) {
                    yy--;
                }
                options.push({"title":yy + "年" + seasons_p[index] + "月","value":yy + "-" + seasons_p[index].split("~")[1]});
                j++;
                if (index <= 0) {
                    index = 3;
                } else {
                    index--;
                }
            }
        } else if (type == "semiyearly") {//半年
            var yy = year;
            var temp = mon % 6;
            if (temp == 1) {//1,7可考核
                index = mon / 6;
            } else {
                index = (mon - temp) / 6;
            }
            index = parseInt(index);
            var j = 0;
            if (temp == 1) {
                options.push({"title":"当前考核周期","value":"-1"});
                index--;
            }
            if (index < 0) {
                index = 1;
                yy--;
            }
            while (j < 3) {
                if (index <= 0) {
                    yy = yy - 1;
                }
                options.push({"title":yy + "年" + semiyearly_p[index] + "月","value":yy + "-" + semiyearly_p[index].split("~")[1]});
                if (index <= 0) {
                    index = 2;
                }
                j++;
                index--;
            }
        } else if (type == "annually") {//一年
            var yy = year;
            if (mon == 1) {//仅一月可考
                options.push({"title":"当前考核周期","value":"-1"});
                yy--;
            }
            for (var i = 0; i < 3; i++) {
                yy--;
                options.push({"title":yy + "年1~12月","value":yy + "-12"});
            }
        }
        return options;
    }

    this.options = getOptionsByType(this.type, this.m, this.y);
}
</script>
